package com.yeeKa.service.impl;

import java.util.*;

import com.yeeKa.common.annotation.DataSource;
import com.yeeKa.common.enums.DataSourceType;
import com.yeeKa.common.exception.BusinessException;
import com.yeeKa.config.NacosConfigLocalCatch;
import com.yeeKa.domain.Lock1518;
import com.yeeKa.domain.Order;
import com.yeeKa.domain.Product;
import com.yeeKa.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yeeKa.mapper.InterlockingRecordsMapper;
import com.yeeKa.domain.SaleTable;
import com.yeeKa.service.InterlockingRecordsService;

import javax.annotation.Resource;

import static com.yeeKa.common.exception.ExceptionCast.assertNullSize;
import static com.yeeKa.common.exception.ExceptionCast.assertNullString;

/**
 * 销售单Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-09-04
 */
@Service
public class InterlockingRecordsImpl implements InterlockingRecordsService
{
    @Resource
    private InterlockingRecordsMapper interlockingRecordsMapper;

    @Autowired
    private final NacosConfigLocalCatch nacosConfigLocalCatch;

    HashMap<String,String> nacos_two;
    HashMap<String,String> nacosThree;

    public InterlockingRecordsImpl(NacosConfigLocalCatch nacosConfigLocalCatch) {
        this.nacosConfigLocalCatch = nacosConfigLocalCatch;
        String nacos_one = nacosConfigLocalCatch.get("nacos_one", String.class);
        nacos_two = nacosConfigLocalCatch.get("nacos_two", HashMap.class);
        nacosThree = nacosConfigLocalCatch.get("nacos_three", HashMap.class);
    }
    /**
     * 查询销售单
     * 
     * @param id 销售单主键
     * @return 销售单
     */
    @Override
    public SaleTable selectSaleTableById(Integer id)
    {
        return interlockingRecordsMapper.selectSaleTableById(id);
    }

    /**
     * 根据订单号获取订单信息
     * 
     * @param saleTable 销售单
     * @return 销售单
     */
    @Override
    @DataSource(DataSourceType.OA)
    public List<SaleTable> selectSaleTableList(SaleTable saleTable)
    {
        return interlockingRecordsMapper.selectSaleTableList(saleTable);
    }

    /**
     * 查询该型号的检验标准
     *
     * @param model 型号
     * @return 销售单集合
     */
    @Override
    @DataSource(DataSourceType.OA)
    public List<Object> selectMapByModel2(String model) {
        ArrayList<Object> arrayBigList = new ArrayList<>();
        HashMap<String, Object> hashBigMap = new HashMap<>(16);
        arrayBigList.add(hashBigMap);
        HashMap map2 = nacosConfigLocalCatch.get("nacos_two", HashMap.class);
        List<String> appearanceList =  (List<String>) map2.get("appearance");
        HashSet<String> appearanceSet= new HashSet<>(appearanceList);
        List<String> structureList =   (List<String>) map2.get("structure");
        HashSet<String> structureSet= new HashSet<>(structureList);
        List<String> functionalityList =   (List<String>) map2.get("functionality");
        HashSet<String> functionalitySet= new HashSet<>(functionalityList);
        ArrayList<Object> appearanceArrayList = new ArrayList<>();
        ArrayList<Object> structureArrayList = new ArrayList<>();
        ArrayList<Object> functionalityArrayList = new ArrayList<>();
        HashMap map3 = nacosConfigLocalCatch.get("nacos_three", HashMap.class);
        hashBigMap.put("appearance",appearanceArrayList);
        hashBigMap.put("structure",structureArrayList);
        hashBigMap.put("functionality",functionalityArrayList);
        if (StringUtils.isEmpty(model)) {
            return arrayBigList;
        }
        String s = interlockingRecordsMapper.selectInspectionStandardsMap(model);
        assertNullString(s,"检验标准未设置类型");
        String[] split = s.split(",");
        for (String s1 : split) {
            HashMap<String, String> hashMap = new HashMap<>(16);
            String s2 = (String) map3.get(s1);
            hashMap.put("key",s1);
            hashMap.put("prop",s1);
            hashMap.put("label",s2);
            if (appearanceSet.contains(s1)) {
                appearanceArrayList.add(hashMap);
            }
            if (structureSet.contains(s1)) {
                structureArrayList.add(hashMap);
            }
            if (functionalitySet.contains(s1)) {
                functionalityArrayList.add(hashMap);
            }
        }

        return arrayBigList;
    }

    /**
     * 查询该型号的检验标准
     *
     * @param model 型号
     * @return 销售单集合
     */
    @Override
    @DataSource(DataSourceType.OA)
    public List<Object> selectMapByModel(String model) {
        ArrayList<Object> arrayBigList = new ArrayList<>();
        HashMap<String, Object> hashBigMap = new HashMap<>(16);
        ArrayList<Object> appearanceArrayList = new ArrayList<>();
        ArrayList<Object> structureArrayList = new ArrayList<>();
        ArrayList<Object> functionalityArrayList = new ArrayList<>();
        hashBigMap.put("appearance",appearanceArrayList);
        hashBigMap.put("structure",structureArrayList);
        hashBigMap.put("functionality",functionalityArrayList);
        arrayBigList.add(hashBigMap);
        if (StringUtils.isEmpty(model)) {
            return arrayBigList;
        }
        List<String> strings = interlockingRecordsMapper.selectInspectionStandardsMap2(model);
        assertNullSize(strings.size(),"检验标准未设置类型");
        for (int i = 0; i < strings.size(); i++) {

            HashMap<String, String> hashMap = new HashMap<>(16);
            hashMap.put("key", "a"+ (i + 1));
            hashMap.put("prop", "a"+ (i + 1));
            hashMap.put("label",strings.get(i).split(":")[1]);
            if (strings.get(i).contains("外观")) {
                appearanceArrayList.add(hashMap);
            }
            if (strings.get(i).contains("功能性检测")) {
                functionalityArrayList.add(hashMap);
            }
            if (strings.get(i).contains("结构性检测")) {
                structureArrayList.add(hashMap);
            }
        }
        return arrayBigList;
    }

    @Override
    @DataSource(DataSourceType.OA)
    public int insertLock1518(Lock1518 lock1518) {
        return interlockingRecordsMapper.insertLock1518(lock1518);
    }

    @Override
    @DataSource(DataSourceType.OA)
    public List<Lock1518> getRecords(String sn, String orderNo,String englishName) {
        return interlockingRecordsMapper.getRecords(sn,orderNo,englishName);
    }

    @Override
    @DataSource(DataSourceType.OA)
    public List<Order> selectOrderList(String salNo) {
        //判断是否是物联锁/品保部门
        checkSalNo(salNo);
        List<Order> orders = interlockingRecordsMapper.selectOrderList();

        //todo 临时数据,需删除
        //Order order = new Order();
        //Order order2 = new Order();
        //Order order3 = new Order();
        //order.setOrderNo("SOWL2107010001");
        //order2.setOrderNo("SOWY2210260002");
        //order3.setOrderNo("SOWL2210200001");
        //orders.add(order);
        //orders.add(order2);
        return orders;
    }

    private void checkSalNo(String salNo) {
        //获取部门的set集合
        Set<String> strings = interlockingRecordsMapper.selectFdStaffNo();
        if (!strings.contains(salNo)) {
            throw new BusinessException(50, "需要让品保或者物联锁部门进行操作");
        }
    }

    /**
     * 新增销售单
     * 
     * @param saleTable 销售单
     * @return 结果
     */
    @Override
    public int insertSaleTable(SaleTable saleTable)
    {
        return interlockingRecordsMapper.insertSaleTable(saleTable);
    }

    /**
     * 修改销售单
     * 
     * @param saleTable 销售单
     * @return 结果
     */
    @Override
    public int updateSaleTable(SaleTable saleTable)
    {
        return interlockingRecordsMapper.updateSaleTable(saleTable);
    }

    /**
     * 批量删除销售单
     * 
     * @param ids 需要删除的销售单主键
     * @return 结果
     */
    @Override
    public int deleteSaleTableByIds(Integer[] ids)
    {
        return interlockingRecordsMapper.deleteSaleTableByIds(ids);
    }

    /**
     * 删除销售单信息
     * 
     * @param id 销售单主键
     * @return 结果
     */
    @Override
    public int deleteSaleTableById(Integer id)
    {
        return interlockingRecordsMapper.deleteSaleTableById(id);
    }

    @Override
    @DataSource(DataSourceType.MASTER)
    public List<Product> selectProductList(Product product) {
        return interlockingRecordsMapper.selectProductList(product);
    }
}
